package com.ivy.wechatforclient.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ivy.wechatforclient.R;
import com.ivy.wechatforclient.entity.User;
import com.ivy.wechatforclient.netConnection.RegisterService;
import com.ivy.wechatforclient.utils.HttpUtil;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lenovo on 2017/11/3.
 */

public class RegisterActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private EditText et_telephone;
    private String path = "http://192.168.49.36:8080/WeChatForServer/UserServlet";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        et_telephone = (EditText) findViewById(R.id.et_verify);

    }
    Map<String ,String > params = new HashMap<String, String>();
    public void commit(View view) throws UnsupportedEncodingException {
        final String username = et_username.getText().toString().trim();
        final String password = et_password.getText().toString().trim();
        final String telephone = et_telephone.getText().toString().trim();
        params.put("username",username);
        params.put("password",password);
        params.put("telephone",telephone);

        new Thread(){
            @Override
            public void run() {
                final String result = HttpUtil.postByHttpClient(path,params,"insert");
                if(result!=null){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent data = new Intent();
                            data.putExtra("username",username);
                            data.putExtra("password",password);
                            data.putExtra("telephone",telephone);
                            setResult(2,data);
                            finish();
                            //Toast.makeText(RegisterActivity.this, result, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this, "注册失败...", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }.start();
    }

}
